//
//  TMTradeCollectController.m
//  国学商家管理
//
//  Created by Tao Li on 2018/7/2.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "TMTradeCollectController.h"
#import "TMTradeCollectCell.h"
#import "TMTradeDetailController.h"
#import "TMUserViewModel.h"
#import "WZNoResultView.h"
#import "TMCollectionModel.h"
#import "TMMyCourseModel.h"
#import "TMCommodityDetailController.h"
@interface TMTradeCollectController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet UICollectionView *tradeCollection;
@property (nonatomic, assign) CGSize itemSize;
@property (nonatomic, strong) TMUserViewModel *viewModel;
@property (nonatomic, strong) NSMutableArray *listArray;
@property (weak, nonatomic) IBOutlet UIView *editView;
@property (weak, nonatomic) IBOutlet UIButton *selectAllBtn;
@property (nonatomic, strong) WZNoResultView *noResultView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottonConst;
@property (nonatomic, assign) BOOL canSelect;
@property (nonatomic, assign) BOOL is_AllEdit;
@property (nonatomic, strong) NSMutableArray *selectIndex;
@property (nonatomic, assign) NSInteger pageNum;
@property (nonatomic, assign) NSInteger totalNum;
@end

@implementation TMTradeCollectController
- (WZNoResultView *)noResultView{
    if (!_noResultView) {
        _noResultView = [[[NSBundle mainBundle] loadNibNamed:@"WZNoResultView" owner:nil options:nil]firstObject];
        if (kWindowH == 812.0) {
            _noResultView.frame = CGRectMake(0, 0, kWindowW, kWindowH);
        }else {
            _noResultView.frame = CGRectMake(0, 0, kWindowW, kWindowH);
        }
        _noResultView.isSearchVC = YES;
        [self.tradeCollection addSubview:_noResultView];
    }
    return _noResultView;
}
- (void)noResultViewShowORNot{
    if (!self.listArray.count) {
        self.noResultView.hidden = NO;
    }else{
        self.noResultView.hidden = YES;
    }
}
- (NSMutableArray *)listArray{
    if (!_listArray) {
        _listArray = [NSMutableArray array];
    }
    return _listArray;
}
- (NSMutableArray *)selectIndex{
    if (!_selectIndex) {
        _selectIndex = [NSMutableArray array];
    }
    return _selectIndex;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = self.isMyCourse ? @"我的课程": @"商品收藏";
    if (self.isMyCourse == NO) {
        [self displayRightItem];
        self.is_AllEdit = NO;
        self.canSelect = NO;
    }
    self.viewModel = [[TMUserViewModel alloc]init];
    self.tradeCollection.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self.listArray removeAllObjects];
        if (self.isMyCourse == YES) {
            [self.viewModel getMyCourseSuccess:^(id responseObject) {
                self.listArray = responseObject;
                [self.tradeCollection.mj_header endRefreshing];
                [self noResultViewShowORNot];
                [self.tradeCollection reloadData];
            } failed:^(NSString *error) {
                [self.tradeCollection.mj_header endRefreshing];
                [self noResultViewShowORNot];
                [self.tradeCollection reloadData];
            }];
        }else{
            self.pageNum =1;
            [self.viewModel getCommityCollectionPage:self.pageNum Success:^(id responseObject, id total) {
                self.listArray = responseObject;
                self.totalNum = [total integerValue];
                [self noResultViewShowORNot];
                [self.tradeCollection reloadData];
                [self.tradeCollection.mj_header endRefreshing];
                [self.tradeCollection.mj_footer endRefreshing];
            } failed:^(NSString *error) {
               [self.tradeCollection.mj_header endRefreshing];
                [self.tradeCollection.mj_footer endRefreshing];
               [self noResultViewShowORNot];
               [self.tradeCollection reloadData];
            }];
        }
    }];
    [self.tradeCollection.mj_header beginRefreshing];
    if (self.isMyCourse == NO) {
        self.tradeCollection.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            self.pageNum ++;
            if (self.pageNum > self.totalNum) {
                [self.tradeCollection.mj_footer endRefreshingWithNoMoreData];
                return;
            }
            [self.viewModel getCommityCollectionPage:self.pageNum Success:^(id responseObject, id total) {
                self.listArray = responseObject;
                [self.tradeCollection reloadData];
                [self.tradeCollection.mj_header endRefreshing];
                [self.tradeCollection.mj_footer endRefreshing];
            } failed:^(NSString *error) {
                if (self.pageNum > 1) {
                    self.pageNum--;
                }
                [self.tradeCollection.mj_header endRefreshing];
                [self.tradeCollection.mj_footer endRefreshing];
                [self.tradeCollection reloadData];
            }];
        }];
    }
}
- (void)displayRightItem{
    UIBarButtonItem *letfItem = [[UIBarButtonItem alloc]initWithTitle:@"编辑"  style:UIBarButtonItemStylePlain target:self action:@selector(addTeacher)];
    self.navigationItem.rightBarButtonItem = letfItem;
}
- (void)addTeacher{
    if (self.listArray.count == 0) {
        return;
    }
    if (self.bottonConst.constant == 0) {
        return;
    }
    [UIView animateWithDuration:0.25 animations:^{
        self.bottonConst.constant = 0;
        self.canSelect = YES;
        self.is_AllEdit = NO;
        [self.tradeCollection reloadData];
    } completion:^(BOOL finished) {
        [self.view layoutIfNeeded];
    }];
}

- (void)setTradeCollection:(UICollectionView *)tradeCollection{
    _tradeCollection = tradeCollection;
    [_tradeCollection registerNib:[UINib nibWithNibName:@"TMTradeCollectCell" bundle:nil] forCellWithReuseIdentifier:@"TMTradeCollectCell"];
    _tradeCollection.delegate = self;
    _tradeCollection.dataSource = self;
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.minimumLineSpacing = 17;
    flowLayout.minimumInteritemSpacing = 0;
    _itemSize = CGSizeMake(173 * kWindowW /375, 226 * kWindowW /375);
    _tradeCollection.collectionViewLayout = flowLayout;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return self.itemSize;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(10, 12, 0, 12);
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.listArray.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    TMTradeCollectCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TMTradeCollectCell" forIndexPath:indexPath];
    if (self.isMyCourse == NO) {
        TMCollectionModel *model = self.listArray[indexPath.row];
        cell.editBtn.hidden = !self.canSelect ? YES :NO;
        cell.editBtn.selected = self.is_AllEdit ? YES :NO;
        cell.collModel = model;
        cell.block = ^(BOOL select){
            //取消收藏。
            if (select == YES) {
                [self.selectIndex addObject:@(model.collid)];
            }else{
                [self.selectIndex removeObject:@(model.collid)];
            }
            if (self.is_AllEdit == YES) {
                //全选了 之后 取消某一个或者多个
                if (select == NO) {
                    [self.selectIndex removeObject:@(model.collid)];
                    if (self.selectIndex.count == 0) {
                        [UIView animateWithDuration:0.25 animations:^{
                            self.bottonConst.constant = 49;
                            self.canSelect = NO;
                            self.is_AllEdit = NO;
                            self.selectAllBtn.selected = NO;
                            [self.tradeCollection reloadData];
                        } completion:^(BOOL finished) {
                            [self.view layoutIfNeeded];
                        }];
                    }
                }
            }
        };
    }else{
        
    TMMyCourseModel *model = self.listArray[indexPath.row];
    cell.model = model;
    }
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (self.isMyCourse == YES) {
        //课程详细
        TMMyCourseModel *models = self.listArray[indexPath.row];
        TMTradeDetailController *vc = [[UIStoryboard storyboardWithName:@"Mine" bundle:nil] instantiateViewControllerWithIdentifier:@"TMTradeDetailController"];
        vc.cid = models.cid;
        vc.order_id = models.order_id;
        vc.isorder = NO;
        [self.navigationController pushViewController:vc animated:YES];
    }else{
       TMCollectionModel *model = self.listArray[indexPath.row];
        TMCommodityDetailController *vc = [[UIStoryboard storyboardWithName:@"Nearby" bundle:nil] instantiateViewControllerWithIdentifier:@"TMCommodityDetailController"];
        vc.rid = model.collid;
        [self.navigationController pushViewController:vc animated:YES];
    }
}
- (IBAction)selectAllAction:(UIButton *)sender {
    sender.selected = !sender.selected;
    self.is_AllEdit = sender.selected;
    for (TMCollectionModel *modes in self.listArray) {
        [self.selectIndex addObject:@(modes.collid)];
    }
    [self.tradeCollection reloadData];
    
}
- (IBAction)cancelCollection:(UIButton *)sender {
    NSLog(@"%@", self.selectIndex);
    [UIView animateWithDuration:0.25 animations:^{
        self.bottonConst.constant = 49;
        self.canSelect = NO;
        self.is_AllEdit = NO;
        self.selectAllBtn.selected = NO;
        [self.tradeCollection reloadData];
    } completion:^(BOOL finished) {
        [self.view layoutIfNeeded];
    }];
    NSString *json = [SPToolSet arrayToJSONString:self.selectIndex];
    [self.viewModel deleteCommityCollection:json Success:^(id responseObject) {
        [self.selectIndex removeAllObjects];
        [self.tradeCollection.mj_header beginRefreshing];
    } failed:^(NSString *error) {
        [self.selectIndex removeAllObjects];
    }];
}
- (IBAction)cancelEditaction:(UIButton *)sender {
    [UIView animateWithDuration:0.25 animations:^{
        self.bottonConst.constant = 49;
        self.canSelect = NO;
        self.is_AllEdit = NO;
        self.selectAllBtn.selected = NO;
         [self.selectIndex removeAllObjects];
        [self.tradeCollection reloadData];
    } completion:^(BOOL finished) {
        [self.view layoutIfNeeded];
    }];
}

@end
